﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.DAL;

namespace MINERP.PrintManage
{
    public partial class FrmAddPaperLength : Form
    {
        public delegate void LinkGridView(int paperid);
        public delegate  void RefreshPaperInfo();
        public static RefreshPaperInfo refreshPaperInfo;

        public LinkGridView linkGridView;
        public int PaperId { get; set; }
        public int PrinterId { get; set; }
        public FrmAddPaperLength()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtPaperLength.Text))
            {
                if (DialogResult.OK == MessageBox.Show("确定添加纸长度?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
                    {
                        PaperInfo pi = entities.PaperInfo.FirstOrDefault(p => p.PrintPaperId == PaperId);
                        if (pi != null)
                        {
                            try
                            {
                                //if (!pi.paperFaceReference.IsLoaded)
                                //{
                                //    pi.paperFaceReference.Load();
                                //}
                                //if (!pi.paperTypeReference.IsLoaded)
                                //{
                                //    pi.paperTypeReference.Load();
                                //}
                                pi.Length += Math.Round(Convert.ToDouble(txtPaperLength.Text) * 100, 2);
                                pi.LastLength += Math.Round(Convert.ToDouble(txtPaperLength.Text) * 100, 2);
                                entities.SaveChanges();
                                this.Hide();
                                ((FrmMain)this.Owner).PreRemainLength = (double)pi.LastLength;
                                linkGridView.Invoke(PrinterId);
                                refreshPaperInfo.Invoke();
                                MessageBox.Show("添加纸成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                this.Close();
                            }
                            catch(Exception ex)
                            {
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("请输入要添加纸的长度!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }          
        }
    }
}
